// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * Utility functions for dealing with portable date formats.  The Javascript
 * Date class, goog.date.DateTime, ringo/utils/dates functions, and Java
 * Date/DateFormat classes all have different formats can parse.
 *
 *  Most commonly parsable format:
 *    2011-06-04T01:13:50-04:00
 *
 * This format will be considered canonical.  This will be the one used for
 * transmision of dates to and from the server.
 *
 *  Closure - From string:
 *    var date = new goog.date.DateTime(new Date(str));
 *
 *  Closure - To string:
 *    var str = date.toIsoString(true, true).replace(/ /, 'T');
 *
 *  Ringo - From string:
 *    var date = require('ringo/utils/dates').parse(str);
 *
 *  Ringo - To string:
 *    var str = require('ringo/utils/dates').toISOString(date);
 */

goog.provide('rb.utils.dates');

goog.require('goog.date.DateTime');


/**
 * @param {string} str Date string of the form "2011-06-04T01:13:50-04:00".
 * @return {goog.date.DateTime} A DateTime object of the given string.
 */
rb.utils.dates.toDateTime = function(str) {
  return new goog.date.DateTime(new Date(str));
};


/**
 * @param {goog.date.DateTime} dateTime A DateTime object to stringify.
 * @return {string} Date string of the form "2011-06-04T01:13:50-04:00".
 */
rb.utils.dates.toIsoString = function(dateTime) {
  return dateTime.toIsoString(true, true).replace(/ /, 'T');
};
